<!-- 试卷列表的首页 -->
<template>
    <div class="app-container">
        <!-- 查询的参数 -->
        <div style="text-align: center;">
            <el-form :model="queryParam" ref="queryForm" :inline="true">
                <el-form-item label="试卷名称：" style="margin-left:40px" >
                    <el-input v-model="queryParam.examName" placeholder="请输入试卷名称"  clearable></el-input>
                </el-form-item>
                <el-form-item label="学科：" style="margin-left:40px">
                    <el-select v-model="queryParam.subjectId" placeholder="请选择科目" clearable  remote reserve-keyword filterable
                    :remote-method="getSubjectName"    >
                    <el-option v-for="item in options" :key="item.value" :label="item.name" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item style="margin-left:20px">
                    <el-button type="primary" @click="submitForm" icon="el-icon-search" round>查询</el-button>
                </el-form-item>
                <el-form-item style="margin-left:20px">
                    <el-button type="warning" @click="handleAdd" icon="el-icon-plus" round>新增试卷</el-button>
                </el-form-item>
            </el-form>
        </div>
        <!-- 试卷列表 -->
    <el-table v-loading="listLoading" :data="tableData" border fit highlight-current-row >
        <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>  
        <el-table-column prop="examName"  align="center" label="考试名称" width="200px"  />
        <el-table-column prop="subjectName"  align="center" label="学科" width="150px" />
        <el-table-column prop="classesName"  align="center" label="考试班级" width="300px" />
        <el-table-column prop="examTotal"   align="center" label="总分" width="80px" />
        <el-table-column prop="beginTime"  align="center" label="开始时间" width="155px"/>
        <!-- <el-table-column prop="endTime"  align="center" label="结束时间" width="160px"/> -->
        <el-table-column prop="username"  align="center" label="创建人" width="100px"/>
        <el-table-column prop="createTime"  align="center" label="创建时间" width="155px"/>
        <el-table-column fixed="right"
        align="center"
        label="操作" width="200px"
        >
        <template slot-scope="scope">
            <router-link :to="{name: 'ExamEdit' , params:{id: scope.row.id,message:'view'}}" style="margin-right: 5px;">
                <el-button type="plain" size="mini">查看</el-button>
            </router-link>
            <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <!-- <router-link :to="{name: 'ExamEdit' , params:{id: scope.row.id,message:'edit'}}" >
            </router-link> -->
            <el-button type="danger" size="mini" @click="handleDel(scope.row)" style="margin-left:5px">删除</el-button>
        </template>
    </el-table-column>
    </el-table>
    <el-pagination
            style="textAlign:center;margin-top: 20px;"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryParam.pageIndex"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="queryParam.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>
    <!-- 新增试卷的弹窗 -->
    <el-dialog title="新增试卷" :visible="dialogVisible" width="50%" :before-close="handleClose">
        <el-form :model="addForm" ref="addForm" :inline="true"  :rules="rules">
            <el-form-item label="试卷名称：" prop="examName" class="el-form-margin" >
                <el-input size="mini" v-model="addForm.examName" placeholder="请输入试卷名称"  clearable></el-input>
            </el-form-item>
            <el-form-item label="选择科目：" class="el-form-margin"  prop="subjectId">
                <el-select size="mini" v-model="addForm.subjectId" placeholder="请选择科目" remote reserve-keyword filterable
                :remote-method="getSubjectName">
                    <el-option v-for="item in options" :key="item.value" :label="item.name" :value="item.value+'.'+item.name"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="组卷方式：" class="el-form-margin" prop="generateType" >
                <el-radio-group v-model="addForm.generateType">
                    <el-radio :label="1">手工组卷</el-radio>
                    <el-radio :label="2">系统组卷</el-radio>
                    <!-- <el-radio :label="3">智能组卷</el-radio> -->
                </el-radio-group>
            </el-form-item>
            <br>
            <el-form-item style="margin-left: 25%;">
                <el-button @click="nextStep" type="primary">下一步</el-button>
                <el-button @click="handleClose" type="plain" style="margin-left:50px">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    </div>
</template>

<script>
import {getSubjectName} from '@/api/subject/subject'
import {pagesList,delById} from '@/api/exam/exam'
export default {
    name:'ExamList',
    data(){
        return{
            // 查询的参数
            queryParam: {
                examName: null,
                subjectId:null,
                pageIndex: 1,
                pageSize: 10,
                deleted: false
            },
            listLoading: false,
            // 表单数据
            tableData: [],
            total:0,
            //查询出来的科目的列表
            options:[],
            //弹窗的是否可见
            dialogVisible:false,
            // 对于新增试卷的表单
            addForm:{
                examName:'',
                subjectId:null,
                // 试卷生成方式
                generateType:1
            },
            // 新增试卷的验证规则
            rules:{
                examName:[{required:true,message:'请输入试卷名',trigger:'blur'}],
                subjectId:[{required:true,message:'请选择科目',trigger:'blur'}],
                generateType:[{required:true,message:'请选择组卷方式',trigger:'blur'}]
            }
        }
    },
    mounted(){
        this.search();
    },
    methods:{
        handleEdit(row){
            if(new Date() > new Date(row.beginTime)){
                this.$message.warning('只能对还未进行考试的试卷进行编辑，请重新选择');
                return;
            }
            this.$router.push({name: 'ExamEdit' , params:{id: row.id,message:'edit'}})
        },
        async search(){
            let  res = await pagesList(this.queryParam);
            this.tableData = res.data.records;
            this.total = res.data.total;
        },
        submitForm(){
            this.search();
        },
        // 处理删除
        handleDel(row){
            if(new Date() > new Date(row.beginTime)){
                this.$message.warning('只能对还未进行考试的试卷进行删除，请重新选择');
                return;
            }
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                    delById(row.id).then(resp =>{
                        if(resp.code == 200){
                            this.$message.success(resp.message);
                        }else{
                            this.$message.warning(resp.message);
                        }
                    })
                    this.search();
                }).catch(()=>{
                    this.$message.info("取消删除");
                })
        },
        // 得到科目的数据
        getSubjectName(val){
            if(val.trim() != ''){
                getSubjectName(val).then(resp =>{
                    this.options = resp.data;
                })
            }
        },
        // 分页相关
        handleSizeChange(val) {
            this.queryParam.pageSize = val;
            this.search();
        },
        handleCurrentChange(val) {
            this.queryParam.pageIndex = val;
            this.search();
        },
        // 处理新增试卷
        handleAdd(){
            this.dialogVisible = true;
        },
        nextStep(){
            this.$refs.addForm.validate(valid=>{
                if(valid){
                    if(this.addForm.generateType == 1){
                        this.$router.push({name:'ExamEdit',
                        params:{
                                examName:this.addForm.examName,
                                subject:this.addForm.subjectId,
                                generateType:this.addForm.generateType,
                                id: 0,
                                message: 'add'
                            }
                        });
                    }else{
                        this.$router.push({name:'AutoGroup',
                            params:{
                                examName:this.addForm.examName,
                                subject:this.addForm.subjectId,
                                generateType:this.addForm.generateType,
                                id: 0,
                                message: 'add'
                            }
                        })
                    }
                }
            })
        },
        // 处理弹窗相关
        handleClose(){
            this.addForm.examName = '';
            this.addForm.subjectId = null;
            this.addForm.generateType = 1;
            this.dialogVisible = false;
        }
    }
}
</script>

<style scoped>
    .el-form-margin{
        margin-left: 40px;
    }

</style>